
/**
 *  This class is the main class of the "World of Zuul" application. 
 *  "World of Zuul" is a very simple, text based adventure game.  Users 
 *  can walk around some scenery. That's all. It should really be extended 
 *  to make it more interesting!
 * 
 *  To play this game, create an instance of this class and call the "play"
 *  method.
 * 
 *  This main class creates and initialises all the others: it creates all
 *  rooms, creates the parser and starts the game.  It also evaluates and
 *  executes the commands that the parser returns.
 * 
 * @author  Michael Kölling and David J. Barnes
 * @version 2016.02.29
 */

public class Game 
{
    private Parser parser;
    private Room currentRoom;
    private Player player;
        
    /**
     * Create the game and initialise its internal map.
     */
    public Game() 
    {
        createRooms();
        parser = new Parser();
        player = new Player();
    }

    /**
     * Create all the rooms and link their exits together.
     */
    private void createRooms()
    {
        Room outside, theater, pub, lab,office,lecture,canteen;
      
        // create the rooms
            outside = new Room("迷宫的入口");
            theater = new Room("火焰山");
            pub = new Room("丛林探险",new Thing("云月草", 50));
            lab = new Room("毒雾‘小心这里的一草一木，它可能会伤害到你’",new Thing("毒尾牙", -20));
            office = new Room("回旋阶梯");
            lecture = new Room("无人之境中心区‘在这里你将遇到终极宝物’", new Thing("冰心之花", 100));
            canteen = new Room("无人之境出口");
            //dorm = new Room("in the dorm room");
            
            // initialise room exits
            //outside.setExits(null, theater, lab, pub, null, null);
            outside.setExit("east", theater);
            outside.setExit("north", pub);
            outside.setExit("west", lab);
            
            //theater.setExits(null, null, null, outside, lecture, null);
            theater.setExit("north", pub);
            theater.setExit("up", canteen);
            // lecture.setExits(null, null, null, null, null, theater);
            // lecture.setExit("down", theater);
            // pub.setExits(null, outside, null, null, null, null);
            pub.setExit("north", lecture);
            pub.setExit("up", office);
            pub.setExit("west", lab);
            lecture.setExit("west", office);
            // lab.setExits(outside, office, null, null, null, null);
            lab.setExit("up", office);
            lab.setExit("down", canteen);
            // lab.setExit("east", office);
            // office.setExits(null, null, null, lab, null, null);
            office.setExit("west", canteen);
            office.setExit("down", lab);
            currentRoom = outside;  // start game outside
    }
    
    public void play(){
        printWelcome();
        boolean finished = false;
        while(!finished){
            Command command = parser.getCommand();
            finished = processCommand(command);
        }
        System.out.println("谢谢你的参与，再见！");
    }
    
    private void printWelcome()
    {
        System.out.println();
        System.out.println("欢迎来到于小桢的迷宫冒险世界——无人之境!");
        System.out.println("这里的迷宫充满了危险和机遇，想要获得珍贵的宝藏吗？那就来闯一闯吧。");
        System.out.println("本游戏一共有三关，出发吧，勇敢的探险者们。");
        System.out.println("假如你需要帮助的话点击'help'即可");
        System.out.println();
        System.out.println("你在" + currentRoom.getDescription());
        currentRoom.printExits();
    }

    /**
     * Given a command, process (that is: execute) the command.
     * @param command The command to be processed.
     * @return true If the command ends the game, false otherwise.
     */
    private boolean processCommand(Command command) 
    {
        boolean wantToQuit = false;
        
         if(command.isUnknown()) {
                System.out.println("我不明白你的意思");
                System.out.println("请重新输入");
                return false;
            }
    
         Word commandWord = command.getCommandWord();
            switch(commandWord){
                    case HELP:
                        printHelp();
                        break;
                    case GO:
                        goRoom(command);
                        break;
                    case QUIT:
                        wantToQuit = quit(command);
                        break;
                    case LOOK:
                        lookThing();
                        break;
                    case PICK:
                        pickThing();
                        break;
                    case CHECK:
                        checkThing();
                        break;
                    case EAT:
                        eatThing(command);
                        break;
        }

        // String commandWord = command.getCommandWord();
        // if (commandWord.equals("help")) {
            // printHelp();
        // }
        // else if (commandWord.equals("go")) {
            // goRoom(command);
        // }
        // else if (commandWord.equals("quit")) {
            // wantToQuit = quit(command);
        // }
        // else if (commandWord.equals("look")) {
            // lookThing();
        // }
        // else if (commandWord.equals("pick")){
            // pickThing();
        // }
        // else if (commandWord.equals("check")) {
            // checkThing();
        // }
        // else if (commandWord.equals("eat")) {
            // eatThing(command);
        // }
        return wantToQuit;
    }
    // implementations of user commands:

    /**
     * Print out some help information.
     * Here we print some stupid, cryptic message and a list of the 
     * command words.
     */
    private void printHelp() 
    {
        System.out.println("你迷路了，你正在漫无目的的行走。");
        System.out.println("请你小心，无人之境危机四伏。");
        System.out.println();
        System.out.println("你的提示命令是：");
        //System.out.println("   go quit help eat look check pick ");
        System.out.println("  " + CommandWords.allCommandWords());
    }

    /** 
     * Try to go in one direction. If there is an exit, enter
     * the new room, otherwise print an error message.
     */
    private void goRoom(Command command) 
    {
        if(!command.hasSecondWord()) {
            // if there is no second word, we don't know where to go...
            System.out.println("现在该去哪里?");
            return;
        }

        String direction = command.getSecondWord();

        // Try to leave current room.
        Room nextRoom = currentRoom.goRoom(direction);
        //Room nextRoom = null;
        /*if(direction.equals("north")) {
            nextRoom = currentRoom.northExit;
        }
        if(direction.equals("east")) {
            nextRoom = currentRoom.eastExit;
        }
        if(direction.equals("south")) {
            nextRoom = currentRoom.southExit;
        }
        if(direction.equals("west")) {
            nextRoom = currentRoom.westExit;
        }
        if(direction.equals("up")) {
            nextRoom = currentRoom.upExit;
        }
        if(direction.equals("down")) {
            nextRoom = currentRoom.downExit;
        }*/

        if (nextRoom == null) {
            System.out.println("这里没有门了!");
        }
        else {
            currentRoom = nextRoom;
            System.out.println("你在" + currentRoom.getDescription());      
            //System.out.print("Exits: ");
            currentRoom.printExits();
            if(currentRoom.getDescription().equals("火焰山")){
               System.out.println("你遇到了火烈鸟并且与它对抗");
               System.out.println("你虽然胜利了但是消耗了体力");
               System.out.println("体力-40");
               System.out.println("你的能量目前是："+player.minStrength());
            }
            else if(currentRoom.getDescription().equals("丛林探险")){
               System.out.println("你遇到了虎妈正在生产，处于最虚弱的时刻");
               System.out.println("你成功获得由它守护的云月草");
               System.out.println("你的能量目前是："+player.getStrength());
            }
            else if(currentRoom.getDescription().equals("毒雾‘小心这里的一草一木，它可能会伤害到你’")){
               System.out.println("你遇到了毒雾，可能因此死亡");
               System.out.println("毒雾会削减你的视力");
               System.out.println("你的能量目前是："+player.getStrength());
            }
            else if(currentRoom.getDescription().equals("无人之境中心区‘在这里你将遇到终极宝物’")){
               System.out.println("你遇到了本迷宫的终极宝藏");
               System.out.println("你成功胜利并且吃下了冰心之花");
               System.out.println("恭喜你成功通关，请获取能量，接下来你将寻找无人之境的出口完成这个游戏。");
               System.out.println("你的能量目前是："+player.getStrength());
            }
            else if(currentRoom.getDescription().equals("无人之境出口")){
               System.out.println("你已经到了无人之境的出口");
               System.out.println("游戏结束");
               System.out.println("你的能量目前是："+player.getStrength());
            }
            else if(currentRoom.getDescription().equals("回旋阶梯")){
               System.out.println("这里是一个过渡区");
               System.out.println("你已经看到了曙光和黑暗，就看你怎么选择了");
               System.out.println("你的能量目前是："+player.getStrength());
            }
            else{
               player.step();
               System.out.println("你的能量目前是："+player.getStrength());
               System.out.println();
            }
    }
}
    
    /** 
     * "Quit" was entered. Check the rest of the command to see
     * whether we really quit the game.
     * @return true, if this command quits the game, false otherwise.
     */
    private boolean quit(Command command) 
    {
        if(command.hasSecondWord()) {
            System.out.println("确定要停止吗?");
            System.out.println("你可以再试一次。");
            return false;
        }
        else {
            return true;  // signal that we want to quit
        }
    }
    
    private void lookThing(){
        System.out.println("你看见地上有" + currentRoom.getThing());
    }
    
    private void pickThing(){
        if(currentRoom.thing==null){
           String c="这里什么都没有，你想捡什么？";
           System.out.println(c);
        }
        else{
        player.pick(currentRoom.getThings());
        System.out.println("你把它从地上捡起来");
        currentRoom.removeThing();
        }
    }
    
    private void checkThing(){
        if(player.bag.isEmpty()){
           System.out.println("这里面什么都没有!");
        }
        else{
           for(String key : player.bag.keySet()){
               System.out.println("你检查了背包里有：" + key);
            }
        }
    }
    
    private void eatThing(Command command){
        if(!command.hasSecondWord()){
            System.out.println("你要吃什么吗？快看看背包里有什么吧。");
            return;
        }
        String food = command.getSecondWord();
        player.eat(food);
    }
}
